package com.hq.sys.controller;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hq.bus.domain.Customer;
import com.hq.sys.constat.SysConstant;
import com.hq.sys.eneity.Menu;
import com.hq.sys.eneity.MenuVo;
import com.hq.sys.eneity.TreeNode;
import com.hq.sys.eneity.User;
import com.hq.sys.mapper.MenuMapper;
import com.hq.sys.service.MenuService;
import com.hq.sys.util.DataGridView;
import com.hq.sys.util.ResultObj;
import com.hq.sys.util.TreeNodeBuilder;
import com.hq.sys.util.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.weekend.Weekend;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @BelongsPackage: com.hq.sys.controller
 * @Author: jiangjunjie
 * @CreateTime: 2024-12-22  09:25
 * @Description: TODO
 * @Version: 1.8
 */
@RestController
@RequestMapping("menu")
public class MenuController {
    @Autowired
    private MenuService menuService;
    @Autowired
    private MenuMapper menuMapper;
   /* @RequestMapping("load")
    public List<TreeNode> load(){
        User user = (User) WebUtils.getHttpServletRequest().getAttribute("user");
        Integer type = user.getType();
        if (Objects.nonNull(type)&&type== SysConstant.USER_TYPE_SUPER){
            List<Mune> munes=munerService.getAllMenu();
        }else{

        }
*/

    @RequestMapping("loadIndexLeftMenuJson")
    public List<TreeNode> loadIndexLeftMenuJson(MenuVo menuVo) {
        //得到当前用户登录的对象
        User user = (User) WebUtils.getHttpSession().getAttribute("user");
        // 查询所有可用的菜单
        Integer type = user.getType();
        List<Menu> menus = null;
        if (Objects.nonNull(type) && type == SysConstant.USER_TYPE_SUPER) {
            menus = this.menuService.queryMenuList();
        }else {
            //TODO ⽤户如果不是管理员,展示的菜单是不同的,添加权限的时候补充
            menus=menuService.queryMenuByUid(user.getUserid());
        }
        //把list的数据放到nodes
        List<TreeNode> nodes = new ArrayList<>();
        for (Menu menu : menus) {
            Boolean spread = menu.getSpread() == 1;
            nodes.add(new TreeNode(menu.getId(), menu.getPid(), menu.getTitle(), menu.getHref(), spread, menu.getTarget(), menu.getIcon()));
        }


        return TreeNodeBuilder.builder(nodes, 1);

    }
    @RequestMapping("loadMenuManagerLeftTreeJson")
    public DataGridView loadAllMenu(MenuVo menuVo){
        List<Menu> menus = menuService.queryAllMenu();
        List<TreeNode> nodes = new ArrayList<>();
        for (Menu menu :menus) {
            Boolean spread = menu.getSpread() == 1;
            nodes.add(new TreeNode(menu.getId(), menu.getPid(), menu.getTitle(), menu.getHref(), spread, menu.getTarget(), menu.getIcon()));
        }
        //返回DataGridView格式的list
        return new DataGridView(nodes);
    }
    //分页
    @RequestMapping("loadAllMenu")
        public DataGridView loadAllMenus(MenuVo rentVo){
        //设置分页
        Page<Object> page = PageHelper.startPage(rentVo.getPage(), rentVo.getLimit());
        Weekend<Menu> weekend = Weekend.of(Menu.class);
        if (StrUtil.isNotBlank(rentVo.getTitle())){
            weekend.weekendCriteria().andLike(Menu::getTitle, "%" + rentVo.getTitle() + "%");
        }
        if (Objects.nonNull(rentVo.getAvailable())){
            weekend.weekendCriteria().andEqualTo(Menu::getAvailable, rentVo.getAvailable());
        }
        List<Menu> data = menuMapper.selectByExample(weekend);
        return new DataGridView(page.getTotal(), data);

    }
    //添加菜单
    @RequestMapping("addMenu")
    public ResultObj addMenu(MenuVo menuVo){
        try {
            this.menuService.addMenu(menuVo);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }
    @RequestMapping("updateMenu")
    public ResultObj updateMenu(MenuVo menuVo){
        try {
            this.menuService.updateMenu(menuVo);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }
    @RequestMapping("deleteMenu")
    public ResultObj deleteMenu(MenuVo menuVo){
        try {
            this.menuService.deleteMenu(menuVo.getId());
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }
    //根据菜单名称查询
    @RequestMapping("queryMenuByTitle")
    public Menu queryMenuByTitle(String title){
        return this.menuService.queryMenuByTitle(title);
    }
    //判断菜单是否有子菜单
    @RequestMapping("checkMenuHasChildren")
    public Menu checkMenuHasChildren(MenuVo menuVo){
        return this.menuService.queryMenuById(menuVo.getId());
    }

}
